#include "Sequence.h"
#include <QString>
#include <Windows.h>
#include<QXmlStreamWriter>
#include <QDomElement>

namespace vkbrd
{
	QMap<int, QString> Sequence::vKeyNames;

	void Sequence::init()
	{
		//insert ascii characters
		for (int i = '!'; i < '~'+1; ++i)
		{
			char name[2];
			name[1] = 0;
			name[0] = (char)i;
			vKeyNames[i] = QString::fromAscii(name);
		}


		vKeyNames[VK_SPACE] = "Space";
		vKeyNames[VK_PRIOR] = "Page Up";
		vKeyNames[VK_NEXT] = "Page Down";
		vKeyNames[VK_END] = "End";
		vKeyNames[VK_HOME] = "Home";
		vKeyNames[VK_LEFT] = "Left Arrow";
		vKeyNames[VK_UP] = "Up Arrow";
		vKeyNames[VK_RIGHT] = "Right Arrow";
		vKeyNames[VK_DOWN] = "Down Arrow";
		vKeyNames[VK_SELECT] = "Select";
		vKeyNames[VK_PRINT] = "Print";
		vKeyNames[VK_EXECUTE] = "Execute";
		vKeyNames[VK_SNAPSHOT] = "Print Screen";
		vKeyNames[VK_INSERT] = "Insert";
		vKeyNames[VK_DELETE] = "Delete";
		vKeyNames[VK_HELP] = "Help";

		vKeyNames[VK_SLEEP] = "Sleep";

		vKeyNames[VK_F1] = "F1";
		vKeyNames[VK_F2] = "F2";
		vKeyNames[VK_F3] = "F3";
		vKeyNames[VK_F4] = "F4";
		vKeyNames[VK_F5] = "F5";
		vKeyNames[VK_F6] = "F6";
		vKeyNames[VK_F7] = "F7";
		vKeyNames[VK_F8] = "F8";
		vKeyNames[VK_F9] = "F9";
		vKeyNames[VK_F10] = "F10";
		vKeyNames[VK_F11] = "F11";
		vKeyNames[VK_F12] = "F12";
		vKeyNames[VK_F13] = "F13";
		vKeyNames[VK_F14] = "F14";
		vKeyNames[VK_F15] = "F15";
		vKeyNames[VK_F16] = "F16";
		vKeyNames[VK_F17] = "F17";
		vKeyNames[VK_F18] = "F18";
		vKeyNames[VK_F19] = "F19";
		vKeyNames[VK_F20] = "F20";
		vKeyNames[VK_F21] = "F21";
		vKeyNames[VK_F22] = "F22";
		vKeyNames[VK_F23] = "F23";
		vKeyNames[VK_F24] = "F24";
	}

	Sequence::Sequence( gInt _combination, int _action )
	{
		combination = _combination;
		action = _action;
	}

	Sequence::Sequence()
	{
		combination = illegalValue, action = 0;
	}

	int Sequence::getSetFlagsCount()
	{
		int count = 0;
		for(int i = 0; i < 10; ++i)
			count += (1 & (combination >> i));
		return count;
	}

	bool Sequence::match( gInt _seq )
	{
		return ((combination & _seq) == combination);
	}

	bool Sequence::match( Sequence& _seq )
	{
		return match(_seq.combination);
	}

	bool Sequence::match( BoolFingers _seq)
	{
		return match(_seq.fin);
	}

	bool Sequence::undefined()
	{
		return illegalValue == combination;
	}

	const QString Sequence::sequenceXmlElementName("sequence");
	const QString combinationXmlAttribName("combination");
	const QString actionXmlAttribName("action");
	
	void Sequence::serialize( QXmlStreamWriter* _writer )
	{
		_writer->writeStartElement(sequenceXmlElementName);
		_writer->writeAttribute(combinationXmlAttribName, QString::number(combination));
		_writer->writeAttribute(actionXmlAttribName, QString::number(action));
		_writer->writeEndElement();
	}

	Sequence::Sequence( QDomElement& _sequenceElement )
	{
		combination = _sequenceElement.attribute(combinationXmlAttribName).toInt();
		action = _sequenceElement.attribute(actionXmlAttribName).toInt();
	}
} //namespace vkbrd
